//
//  XuanzeshouhuoViewController.m
//  tianjin
//
//  Created by 鞠超 on 16/8/10.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "XuanzeshouhuoViewController.h"

#import "XuanzeshouhuoTableViewCell.h"
#import "XinzengshouhuoViewController.h"
#import "BianjishouhuoViewController.h"

@interface XuanzeshouhuoViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIView * view_bottom;

//没有收货地址
@property (nonatomic, strong) UIImageView * image_Mshouhuo;
@property (nonatomic, strong) UILabel * lbl_Mshouhuo;

//有收货地址的
@property (nonatomic, strong) UITableView * tableView;

//记录
@property (nonatomic, assign) NSInteger index_x;
@property (nonatomic, assign) BOOL is_OK;

@end

@implementation XuanzeshouhuoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void)p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    _topView.backgroundColor = [UIColor blackColor];
    
    _lblTitle.text = @"选择收货地址";
    _lblTitle.textColor = [UIColor whiteColor];
    _lblTitle.font = [UIFont systemFontOfSize:19];
    
    UIImageView * image_return = [[UIImageView alloc] initWithFrame:CGRectMake(15, 32, 10, 20)];
    image_return.image = [UIImage imageNamed:@"zuofanhui@2x.png"];
    [_topView addSubview:image_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(10, 20, 35, 44);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//显示tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    //底部栏
    [self p_bottomView];
    
//    //没有收货地址
//    self.image_Mshouhuo = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 40, SCREEN_HEIGHT / 2 - 50, 80, 100)];
//    self.image_Mshouhuo.image = [UIImage imageNamed:@"dadingwei@2x.png"];
//    [self.view addSubview:self.image_Mshouhuo];
//    
//    self.lbl_Mshouhuo = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(self.image_Mshouhuo.frame) + 10, SCREEN_WIDTH - 40, 20)];
//    self.lbl_Mshouhuo.text = @"您还没有收货地址哦~";
//    self.lbl_Mshouhuo.textColor = [UIColor grayColor];
//    self.lbl_Mshouhuo.textAlignment = NSTextAlignmentCenter;
//    [self.view addSubview:self.lbl_Mshouhuo];
    
    //有收获地址的
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 60) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    [self.view addSubview:self.tableView];
    
    //注册
    [self.tableView registerClass:[XuanzeshouhuoTableViewCell class] forCellReuseIdentifier:@"cell_dizhi"];
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 116;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XuanzeshouhuoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_dizhi" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    //删除
    cell.btn_shanchu.tag = indexPath.row + 200;
    [cell.btn_shanchu addTarget:self action:@selector(btn_shanchuAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //编辑
    cell.btn_bianji.tag = indexPath.row + 500;
    [cell.btn_bianji addTarget:self action:@selector(btn_bianjiAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //选择
    cell.btn_select.tag = indexPath.row + 800;
    [cell.btn_select addTarget:self action:@selector(btn_selectAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [cell.btn_select setBackgroundImage:[UIImage imageNamed:@"weixuanzhong@2x.png"] forState:(UIControlStateNormal)];

    if(indexPath.row == self.index_x && self.is_OK == 1)
    {
        [cell.btn_select setBackgroundImage:[UIImage imageNamed:@"xuanzhe_1@2x.png"] forState:(UIControlStateNormal)];
    }
    
    return cell;
}

#pragma mark - 选择默认
- (void )btn_selectAction:(UIButton *)sender
{
    self.index_x = sender.tag - 800;

    if([sender.currentBackgroundImage isEqual:[UIImage imageNamed:@"xuanzhe_1@2x.png"]])
    {
        [sender setBackgroundImage:[UIImage imageNamed:@"weixuanzhong@2x.png"] forState:(UIControlStateNormal)];
        
        self.is_OK = NO;
    }
    else
    {
        [sender setBackgroundImage:[UIImage imageNamed:@"xuanzhe_1@2x.png"] forState:(UIControlStateNormal)];
        
        self.is_OK = YES;
    }

    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        
        [self.tableView reloadData];
    });
}

#pragma mark - 删除
- (void )btn_shanchuAction:(UIButton *)sender
{
    NSInteger count = sender.tag - 200;
    
    NSLog(@"对第%ld个进行操作",count);
    
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定删除该收货地址" preferredStyle:(UIAlertControllerStyleAlert)];
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];
    
    UIAlertAction * action = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alert addAction:action];
    
    UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            //刷新tableView(记住,要更新放在主线程中)
            
            [self.tableView reloadData];
        });
    }];
    
    [alert addAction:action1];
}

#pragma mark - 编辑
- (void )btn_bianjiAction:(UIButton *)sender
{
    NSInteger count = sender.tag - 500;
    
    NSLog(@"对第%ld个进行操作",count);
    
    BianjishouhuoViewController * bianjishouhuoViewController = [[BianjishouhuoViewController alloc] init];
    [self showViewController:bianjishouhuoViewController sender:nil];
}

#pragma mark - 新增收货地址
- (void )btn_xianjianAction:(UIButton *)sender
{
//    NSLog(@"跳转到新增收货地址页面");
    
    XinzengshouhuoViewController * xinzengshouhuoViewController = [[XinzengshouhuoViewController alloc] init];
    [self showViewController:xinzengshouhuoViewController sender:nil];
}

#pragma mark - 底部栏
- (void )p_bottomView
{
    self.view_bottom = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 60, SCREEN_WIDTH, 60)];
    self.view_bottom.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.view_bottom];
    
    UIButton * btn_xianjian = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_xianjian.frame = CGRectMake(10, 10, SCREEN_WIDTH - 20, 40);
    btn_xianjian.layer.cornerRadius = 5;
    btn_xianjian.backgroundColor = navi_bar_bg_color;
    [btn_xianjian setTitle:@"新建收货地址" forState:(UIControlStateNormal)];
    btn_xianjian.titleLabel.font = [UIFont systemFontOfSize:16];
    [btn_xianjian setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [self.view_bottom addSubview:btn_xianjian];
    [btn_xianjian addTarget:self action:@selector(btn_xianjianAction:) forControlEvents:(UIControlEventTouchUpInside)];
}











@end
